{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:57:57.298957900Z",
     "start_time": "2024-09-19T12:57:30.681783200Z"
    }
   },
   "id": "955def2dff9332a5",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:59:48.679140Z",
     "start_time": "2024-09-19T12:59:47.673473200Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  age_range  gender\n0   376517        6.0     1.0\n1   234512        5.0     0.0\n2   344532        5.0     0.0\n3   186135        5.0     0.0\n4    30230        5.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>376517</td>\n      <td>6.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>234512</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>344532</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>186135</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>30230</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = pd.read_csv('user_info_format1.csv')\n",
    "user_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type\n0   328862   323294     833       2882    2661.0         829            0\n1   328862   844400    1271       2882    2661.0         829            0\n2   328862   575153    1271       2882    2661.0         829            0\n3   328862   996875    1271       2882    2661.0         829            0\n4   328862  1086186    1271       1253    1049.0         829            0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>seller_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>328862</td>\n      <td>323294</td>\n      <td>833</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>328862</td>\n      <td>844400</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>328862</td>\n      <td>575153</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>328862</td>\n      <td>996875</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>328862</td>\n      <td>1086186</td>\n      <td>1271</td>\n      <td>1253</td>\n      <td>1049.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log = pd.read_csv('user_log_format1.csv')\n",
    "user_log.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:59:47.621438300Z",
     "start_time": "2024-09-19T12:57:57.291952900Z"
    }
   },
   "id": "2c525cb895b58ac5",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id         0\nage_range    2217\ngender       6436\ndtype: int64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:59:57.865942700Z",
     "start_time": "2024-09-19T12:59:57.667811800Z"
    }
   },
   "id": "2e9d2aa998b4b3b9",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id            0\nitem_id            0\ncat_id             0\nseller_id          0\nbrand_id       91015\ntime_stamp         0\naction_type        0\ndtype: int64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.isnull().sum() "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:00:04.648015300Z",
     "start_time": "2024-09-19T13:00:02.041788200Z"
    }
   },
   "id": "b52a126a0523a131",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424170 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    424170 non-null  int64  \n",
      " 1   age_range  421953 non-null  float64\n",
      " 2   gender     417734 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 9.7 MB\n"
     ]
    }
   ],
   "source": [
    "user_info.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:00:07.195736800Z",
     "start_time": "2024-09-19T13:00:06.610815100Z"
    }
   },
   "id": "7dffd89587d5fc0f",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   seller_id    int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   int64  \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(6)\n",
      "memory usage: 2.9 GB\n"
     ]
    }
   ],
   "source": [
    "user_log.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:00:11.064233900Z",
     "start_time": "2024-09-19T13:00:10.920638900Z"
    }
   },
   "id": "a57b928566bc0c2e",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(54925330, 7)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:00:14.531227800Z",
     "start_time": "2024-09-19T13:00:14.445670900Z"
    }
   },
   "id": "78ef6fdac1a8b2ed",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10464\\1355905500.py:2: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['age_range'].replace(np.nan,2,inplace=True) # 2和NULL表示未知\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10464\\1355905500.py:3: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['gender'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id      0\nage_range    0\ngender       0\ndtype: int64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除空值\n",
    "user_info['age_range'].replace(np.nan,2,inplace=True) # 2和NULL表示未知\n",
    "user_info['gender'].replace(np.nan,-1,inplace=True)\n",
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:00:17.630286700Z",
     "start_time": "2024-09-19T13:00:17.415643500Z"
    }
   },
   "id": "fb4b8f869391b193",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10464\\1715364757.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_log['brand_id'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id        0\nitem_id        0\ncat_id         0\nseller_id      0\nbrand_id       0\ntime_stamp     0\naction_type    0\ndtype: int64"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log['brand_id'].replace(np.nan,-1,inplace=True)\n",
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:00:29.836699300Z",
     "start_time": "2024-09-19T13:00:22.897539200Z"
    }
   },
   "id": "26c9ebd5dccdc3c3",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "13750198\n"
     ]
    }
   ],
   "source": [
    "print(user_info.duplicated().sum())\n",
    "print(user_log.duplicated().sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:02:53.822536400Z",
     "start_time": "2024-09-19T13:00:29.821688900Z"
    }
   },
   "id": "b05a9703db44e02a",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user_log.drop_duplicates(inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:05:58.078890200Z",
     "start_time": "2024-09-19T13:02:53.791017700Z"
    }
   },
   "id": "ea2c96927e2f314b",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label\n0    34176         3906      0\n1    34176          121      0\n2    34176         4356      1\n3    34176         2217      0\n4   230784         4818      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('train_format1.csv')\n",
    "train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:05:58.881922500Z",
     "start_time": "2024-09-19T13:05:58.084893300Z"
    }
   },
   "id": "75111d5dd5f7e736",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender\n0    34176         3906      0        6.0     0.0\n1    34176          121      0        6.0     0.0\n2    34176         4356      1        6.0     0.0\n3    34176         2217      0        6.0     0.0\n4   230784         4818      0        0.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.merge(train,user_info, on='user_id')\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:06:00.409950200Z",
     "start_time": "2024-09-19T13:05:58.835894400Z"
    }
   },
   "id": "216f675c529a3679",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender  item_id  cat_id  brand_id  \\\n0    34176         3906      0        6.0     0.0   757713     821    6268.0   \n1    34176         3906      0        6.0     0.0   718096    1142    6268.0   \n2    34176         3906      0        6.0     0.0   757713     821    6268.0   \n3    34176         3906      0        6.0     0.0   613698     821    6268.0   \n4    34176         3906      0        6.0     0.0   757713     821    6268.0   \n\n   time_stamp  action_type  \n0        1110            0  \n1        1031            3  \n2        1031            3  \n3        1021            0  \n4        1108            0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>718096</td>\n      <td>1142</td>\n      <td>6268.0</td>\n      <td>1031</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1031</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>613698</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1021</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713</td>\n      <td>821</td>\n      <td>6268.0</td>\n      <td>1108</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.rename(columns={'seller_id':'merchant_id'},inplace=True)\n",
    "df_train = pd.merge(df_train,user_log,on=['user_id','merchant_id'],how='left')\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:06:38.902214100Z",
     "start_time": "2024-09-19T13:06:00.047195400Z"
    }
   },
   "id": "f55104e9f1c1a701",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "label\n0    1599925\n1     178522\nName: count, dtype: int64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['label'].value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:06:39.106349Z",
     "start_time": "2024-09-19T13:06:38.876697Z"
    }
   },
   "id": "92134a1df9e4dcf8",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X = df_train.drop('label',axis=1)\n",
    "X = X.drop('user_id',axis=1)\n",
    "y = df_train['label']\n",
    "X_train,X_val,y_train,y_val = train_test_split(X, y, test_size=0.7, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:17:54.855609200Z",
     "start_time": "2024-09-19T13:17:51.956688500Z"
    }
   },
   "id": "231aba80b2fab444",
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import roc_auc_score"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:09:59.741144Z",
     "start_time": "2024-09-19T13:09:54.640695600Z"
    }
   },
   "id": "a1d1ebf5db12cef1",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "model = RandomForestClassifier(max_depth=10, random_state=0,class_weight='balanced')\n",
    "model.fit(X_train,y_train)\n",
    "y_pred=model.predict(X_val)\n",
    "y_proba = model.predict_proba(X_val)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:21:52.785523700Z",
     "start_time": "2024-09-19T13:17:56.233540600Z"
    }
   },
   "id": "d678f0a05dc415b3",
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0.6236838052413748"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auc = roc_auc_score(y_val,y_pred)\n",
    "auc"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:21:53.818209700Z",
     "start_time": "2024-09-19T13:21:52.799034500Z"
    }
   },
   "id": "afc91b7279bb790f",
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "merchant_id: 0.2210733334344043\n",
      "age_range: 0.06378248772806466\n",
      "gender: 0.04768391405583964\n",
      "item_id: 0.05248781338964494\n",
      "cat_id: 0.08984738594625961\n",
      "brand_id: 0.20639799529137057\n",
      "time_stamp: 0.2742484442832818\n",
      "action_type: 0.044478625871134554\n"
     ]
    }
   ],
   "source": [
    "# 打印特征重要性\n",
    "for name, importance in zip(X_train, model.feature_importances_):\n",
    "    print(f\"{name}: {importance}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T13:21:54.351333400Z",
     "start_time": "2024-09-19T13:21:53.824713Z"
    }
   },
   "id": "2413f997a26ec5ba",
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "id": "a2c3dec9ff1ea314",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "id": "a79e389c30e4b712",
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
